Skip to content

Conversation

@aitap
Copy link
Member

@aitap aitap commented Feb 17, 2025

The C standard (before C23) says that giving invalid pointers, even with length=0, to memcpy() is undefined behaviour. R returns an invalid pointer for vectors of length 0, so avoid calls to memcpy() altogether in such cases.

Fixes: #6819

The C standard (before C23) says that giving invalid pointers, even with
length=0, to memcpy() is undefined behaviour. R returns an invalid
pointer for vectors of length 0, so avoid calls to memcpy() altogether
in such cases.
@codecov
Copy link

codecov bot commented Feb 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.64%. Comparing base (f231b5d) to head (4773ceb).
Report is 11 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6820   +/-   ##
=======================================
  Coverage   98.64%   98.64%           
=======================================
  Files          79       79           
  Lines       14642    14650    +8     
=======================================
+ Hits        14444    14452    +8     
  Misses        198      198           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions
Copy link

github-actions bot commented Feb 17, 2025

Comparison Plot

Generated via commit 4773ceb

Download link for the artifact containing the test results: ↓ atime-results.zip

Task Duration
R setup and installing dependencies 4 minutes and 32 seconds
Installing different package versions 8 minutes and 30 seconds
Running and plotting the test cases 2 minutes and 35 seconds

Copy link
Member

@MichaelChirico MichaelChirico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, feel free to merge whether or not you want to make my suggested change.

Do we have any way of preventing this from recurring in the future?

aitap and others added 2 commits February 23, 2025 14:11
Co-authored-by: Michael Chirico <[email protected]>
@aitap
Copy link
Member Author

aitap commented Feb 23, 2025

Maybe not literally preventing, but a sanitizer-enabled CI check should be able to catch similar problems as soon as they land on master.

Then why does rocker/r-devel-san-clang built using the same Debian Testing clang miss this, while our own container doesn't? Not sure. Maybe due to -O3 optimizing some of it away?

@aitap aitap merged commit 299d44d into master Feb 23, 2025
10 of 11 checks passed
@aitap aitap deleted the fix-0len branch February 23, 2025 14:38
aitap added a commit that referenced this pull request May 21, 2025
* Avoid memcpy()-ing 0-length vectors

The C standard (before C23) says that giving invalid pointers, even with
length=0, to memcpy() is undefined behaviour. R returns an invalid
pointer for vectors of length 0, so avoid calls to memcpy() altogether
in such cases.

* Use early returns for 0-length cases

Co-authored-by: Michael Chirico <[email protected]>
TysonStanley pushed a commit that referenced this pull request May 25, 2025
* Avoid memcpy()-ing 0-length vectors

The C standard (before C23) says that giving invalid pointers, even with
length=0, to memcpy() is undefined behaviour. R returns an invalid
pointer for vectors of length 0, so avoid calls to memcpy() altogether
in such cases.

* Use early returns for 0-length cases

Co-authored-by: Michael Chirico <[email protected]>
MichaelChirico added a commit that referenced this pull request May 26, 2025
* Avoid `memcpy()`-ing 0-length vectors (#6820)

* Avoid memcpy()-ing 0-length vectors

The C standard (before C23) says that giving invalid pointers, even with
length=0, to memcpy() is undefined behaviour. R returns an invalid
pointer for vectors of length 0, so avoid calls to memcpy() altogether
in such cases.

* Use early returns for 0-length cases

Co-authored-by: Michael Chirico <[email protected]>

* Avoid memcpy() of empty vector (#6911)

* Avoid memcpy() of empty vector

* in getidcols() too

* Add a test case

Distilled from the `melt()` call here:

https://github.com/gdemin/expss/blob/d498caf72a9938ac71b9e06fd61485cd0347d607/tests/testthat/test_subtotal.R#L161-L166

* NEWS entry

---------

Co-authored-by: Michael Chirico <[email protected]>
Co-authored-by: Michael Chirico <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

growVector, copyAsPlain may formally cause undefined behaviour with 0-length vectors

2 participants